﻿// Copyright (c) 2014 Converter Systems LLC

using ConverterSystems.Workstation.Views;
using Microsoft.Practices.Prism.MefExtensions;
using Microsoft.Practices.Prism.Modularity;
using System;
using System.ComponentModel.Composition.Hosting;
using System.Windows;

namespace ConverterSystems.Workstation
{
    /// <summary>
    /// Runs a bootstrapping sequence that initializes the Prism services.
    /// </summary>
    public class AppBootstrapper : MefBootstrapper, IDisposable
    {
        protected override IModuleCatalog CreateModuleCatalog()
        {
            // Creates a Module Catalog from a list of modules found in the .config file.
            return new ConfigurationModuleCatalog();
        }

        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();
            // Adds a ComposablePartCatalog from the types found in the listed assemblies.
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Shell).Assembly));
        }

        protected override DependencyObject CreateShell()
        {
            // Creates the Shell window.
            return this.Container.GetExportedValue<Shell>();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();
            // Shows the Shell window.
            Application.Current.MainWindow = (Window)this.Shell;
            Application.Current.MainWindow.Show();
        }

        public void Dispose()
        {
            // Disposing the container will dispose all the shared component parts.
            if (this.Container != null)
            {
                this.Container.Dispose();
                this.Container = null;
            }
        }
    }
}